    //
//  BaseController.m
//  NBANews
//
//  Created by Liccon Chang on 11-1-10.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "BaseController.h"


@implementation BaseController

@synthesize topGameInfo;
@synthesize AdvertisHeight;
@synthesize Advertisements;

-init{
	self = [super init];
	if (self) {
		AdvertisHeight = 44;
	}
	return self;
}

- (id)initWithGameInfo:(GameInfo*) aInfo{
	self = [super init];
	if (self) {
		AdvertisHeight = 44;
		topGameInfo = aInfo;
	}
	return self;
}

- (void)loadView {
	[super loadView];
	UIImage *imgBg = [UIImage imageNamed:@"bg.png"];
    UIColor *c = [[UIColor alloc] initWithPatternImage:imgBg];
	self.view.backgroundColor = c;
}

- (void)addNavExit {
	UIButton *menuButton = [UIButton buttonWithType:UIButtonTypeCustom];
	UIImage *img = [UIImage imageNamed:@"blackbutton.png"];
    [menuButton setImage:img forState:UIControlStateNormal];  
	menuButton.frame = CGRectMake(0.0, 0.0, img.size.width, img.size.height);
	UILabel *t = [[UILabel alloc] initWithFrame:menuButton.frame];
	t.backgroundColor = [UIColor clearColor];
	t.textColor = [UIColor whiteColor];
	t.textAlignment = UITextAlignmentCenter;
	t.font = [UIFont boldSystemFontOfSize:13.0f];
	t.text = @"退出";
	[menuButton addSubview:t];
	[t release];
	[menuButton addTarget:self action:@selector(Exit) forControlEvents:UIControlEventTouchUpInside];   
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:menuButton];
    [self.navigationItem setLeftBarButtonItem:barButton];
	[barButton release];
}

- (void)addNavBack {
	UIButton *menuButton = [UIButton buttonWithType:UIButtonTypeCustom];
	UIImage *img = [UIImage imageNamed:@"blackbutton.png"];
    [menuButton setImage:img forState:UIControlStateNormal];  
	menuButton.frame = CGRectMake(0.0, 0.0, img.size.width, img.size.height);
	UILabel *t = [[UILabel alloc] initWithFrame:menuButton.frame];
	t.backgroundColor = [UIColor clearColor];
	t.textColor = [UIColor whiteColor];
	t.textAlignment = UITextAlignmentCenter;
	t.font = [UIFont boldSystemFontOfSize:13.0f];
	t.text = @"返回";
	[menuButton addSubview:t];
	[t release];
	[menuButton addTarget:self action:@selector(Back) forControlEvents:UIControlEventTouchUpInside];   
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:menuButton];
    [self.navigationItem setLeftBarButtonItem:barButton];
	[barButton release];
}

- (void) Exit{
	
}

- (void) Back{
	
}

- (NetworkUtil*)submitURL:(NSString *)address withOperation:(NSString *)operation {
	NSLog(@"login url:%@",address);
	[self ShowWaiteLabel];
	NSURL *url = [NSURL URLWithString:address];
	NetworkUtil *net = [[NetworkUtil alloc] initWithURL:url
										  withOperation:operation
										   withDelegate:self
											 onComplete:@selector(completeFetch:)
												onError:@selector(errorFetch:)];
	
	return net;
}

- (void)completeFetch:(NetworkUtil *)obj{	
	[self HideWaiteLabel];
}

- (void)errorFetch:(NetworkUtil *)obj {
	[self HideWaiteLabel];
	[network release];
	network =nil;
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"网络" message:@"网络错误，请重新操作！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
	[alert show];
	[alert release];
}


- (void)viewWillAppear:(BOOL)animated {
	if (self.tabBarController) {
		self.tabBarController.title = self.title;
	}
	if (self.tabBarController) {
		self.tabBarController.navigationItem.leftBarButtonItem = self.navigationItem.leftBarButtonItem;
	}
}

-(void) InsertAdvertisementsView{
	AdvertisHeight = 44;
	if (Advertisements == nil) {
		CGRect rect = self.view.frame;
		rect.size.height = AdvertisHeight;
		rect.size.width = self.view.frame.size.height;
		rect.origin.x = 0;
		rect.origin.y = self.view.frame.size.width - AdvertisHeight - TabBarHeight - NarBarHeight + StateBarHeight;
		Advertisements = [[UILabel alloc] initWithFrame:rect];
		((UILabel*)Advertisements).textAlignment = UITextAlignmentCenter;
		((UILabel*)Advertisements).textColor = [UIColor whiteColor];
		((UILabel*)Advertisements).text = @"广告位置";
		Advertisements.backgroundColor = [UIColor colorWithRed:210.0f/255.0f green:180.0f/255.0f blue:140.0f/255.0f alpha:1.0f];
		[self.view addSubview:Advertisements];
	}
}

- (void) ShowWaiteLabel{
	if (waiteLabel == nil) {
		int textWidth = 200;
		CGRect rect = CGRectMake((self.view.frame.size.width - textWidth) / 2, self.view.frame.size.height - 100, textWidth, 40);
		waiteLabel = [[UILabel alloc] initWithFrame:rect];
		waiteLabel.textAlignment = UITextAlignmentCenter;
		waiteLabel.textColor = [UIColor whiteColor];
		waiteLabel.text = @"正在取数据，请稍候...";
		waiteLabel.backgroundColor = [UIColor colorWithRed:200.0f/255.0f green:180.0f/255.0f blue:150.0f/255.0f alpha:0.8f];
	}
	[self.view addSubview:waiteLabel];
}

- (void) HideWaiteLabel{
	if (waiteLabel) {
		[waiteLabel removeFromSuperview];
	}
}

-(UIView*) CreateColumnView:(RowData*) aRowData ColWidth:(int) aWidth{
	ColumnView* view = [[ColumnView alloc] initWithFrame:CGRectMake(0, 0, aWidth, 40)];
	UILabel* label = nil;
	for (int i = 0; i < [aRowData.RowTexts count]; i++) {
		label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 80, 40)];
		label.text = [aRowData.RowTexts objectAtIndex:i];
		label.backgroundColor = [UIColor clearColor];
		label.textColor = [UIColor blackColor];
		label.textAlignment = UITextAlignmentCenter;
		label.tag = i;
		[view addSubview:label];
	}
	return view;
}

- (UIView*) CreateGameInfoView:(GameInfo*) aInfo{
	int width = self.view.frame.size.height;
	int height = 60;
	UIView* topGameInfoView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
	UIImage* topBg = [UIImage imageNamed:@"nav_bg.png"];
    UIColor *topbgc = [[UIColor alloc] initWithPatternImage:topBg];
	topGameInfoView.backgroundColor = topbgc;
	
	UILabel* Hometeam = [[UILabel alloc] initWithFrame:CGRectMake(width / 2 - 120, 0, 120, height/2)];
	Hometeam.backgroundColor = [UIColor clearColor];
	Hometeam.textAlignment = UITextAlignmentCenter;
	Hometeam.textColor = [UIColor yellowColor];
	Hometeam.text = aInfo.Hometeam;
	[topGameInfoView addSubview: Hometeam];
	[Hometeam release];
	
	UILabel* HometeamScore = [[UILabel alloc] initWithFrame:CGRectMake(width / 2 - 120, height/2 - 10, 80, height/2)];
	HometeamScore.backgroundColor = [UIColor clearColor];
	HometeamScore.textAlignment = UITextAlignmentCenter;
	HometeamScore.textColor = [UIColor yellowColor];
	HometeamScore.text = aInfo.HometeamScore;
	[topGameInfoView addSubview: HometeamScore];
	[HometeamScore release];
	
	UILabel* gap = [[UILabel alloc] initWithFrame:CGRectMake(width / 2 - 10, height/2 - 20, 20, height/2)];
	gap.backgroundColor = [UIColor clearColor];
	gap.textAlignment = UITextAlignmentCenter;
	gap.textColor = [UIColor yellowColor];
	gap.text = @":";
	[topGameInfoView addSubview: gap];
	[gap release];
	
	UILabel* Visitors = [[UILabel alloc] initWithFrame:CGRectMake(width / 2, 0, 120, height/2)];
	Visitors.backgroundColor = [UIColor clearColor];
	Visitors.textAlignment = UITextAlignmentCenter;
	Visitors.textColor = [UIColor yellowColor];
	Visitors.text = aInfo.Visitors;
	[topGameInfoView addSubview: Visitors];
	[Visitors release];
	
	UILabel* VisitorsScore = [[UILabel alloc] initWithFrame:CGRectMake(width / 2 + 40, height/2 - 10, 80, height/2)];
	VisitorsScore.backgroundColor = [UIColor clearColor];
	VisitorsScore.textAlignment = UITextAlignmentCenter;
	VisitorsScore.textColor = [UIColor yellowColor];
	VisitorsScore.text = aInfo.VisitorsScore;
	[topGameInfoView addSubview: VisitorsScore];
	[VisitorsScore release];
	
	//image
	UIImageView* leftImgView = [[UIImageView alloc] initWithFrame:CGRectMake(40, 0, 80, height)];
	[leftImgView setImage:[UIImage imageNamed:[SystemUtil getTeamImageName:aInfo.Hometeam]]];
	[topGameInfoView addSubview: leftImgView];
	[leftImgView release];
	
	UIImageView* rightImgView = [[UIImageView alloc] initWithFrame:CGRectMake(width - 120, 0, 80, height)];
	[rightImgView setImage:[UIImage imageNamed:[SystemUtil getTeamImageName:aInfo.Visitors]]];
	[topGameInfoView addSubview: rightImgView];
	[rightImgView release];
	
	return topGameInfoView;
}

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationLandscapeRight);
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[NetworkUtil release];
	[Advertisements release];
	[waiteLabel release];
    [super dealloc];
}


@end
